package cn.wolfcode.crm.web.controller;

//日志工具类

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.ISigninService;
import cn.wolfcode.crm.util.CustomException;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PunchCard;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("signin")
public class SigninController {

    @Autowired
    private ISigninService service;

    @RequestMapping("index")
    public  String  index(){
        return "signin/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryForObject qo){
        return service.query(qo);
    }

    @RequestMapping("signinTime")
    @ResponseBody
    //签到
    public JsonResult checkIn() throws Exception
    {
        //获取到当前登录的用户
        try {
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();

            if(employee == null)
            {
                return new JsonResult("为何你这么6,让你签到都不行");
            }

            Date date = new Date();
            SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
            String format = sp.format(date);

            Signin signin = service.selectSigninByEmployeeId(employee.getId(), format);
            service.signinTime(signin, employee);
        } catch (CustomException e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
        JsonResult result = new JsonResult();
        result.setMsg("欢迎回来");
        return result;
    }

    @RequestMapping("signoutTime")
    @ResponseBody
    //签退
    public JsonResult checkOut()
    {
        //获取到当前登录的用户
        try {
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();

            if(employee == null)
            {
                return new JsonResult("为何你这么6,让你签退都不行");
            }

            Date date = new Date();
            SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
            String format = sp.format(date);

            Signin signin = service.selectSigninByEmployeeId(employee.getId(), format);
            service.signoutTime(signin, employee, format);
        } catch (CustomException e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
        JsonResult result = new JsonResult();
        result.setMsg("下班打卡成功");
        return result;
    }

    //根据员工id查询对应的所有考勤记录
    @RequestMapping("querySigninByEmployeeId")
    @ResponseBody
    public PunchCard querySigninByEmployeeId(Long id){

        PunchCard punchCard = null;
        try {
            punchCard = service.querySigninByEmployeeId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new PunchCard("获取考勤记录失败");
        }

        punchCard.setMsg("获取考勤记录成功");
        return punchCard;
    }
}
